package com.jy.wms.controller;

import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.*;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsEstimatedArrivalOrderDetailed;
import com.jy.wms.service.WmsEstimatedArrivalOrderDetailedService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: WmsEstimatedArrivalOrderDetailedController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author Liyi
 * @Date Wed Sep 27 09:49:11 CST 2017
 */
@Controller
@RequestMapping("/wmsEstimatedArrivalOrderDetailed")
@Api(value = "/wmsEstimatedArrivalOrderDetailed", description = "关于预期到货通知单明细操作")
public class  WmsEstimatedArrivalOrderDetailedController extends BaseController<WmsEstimatedArrivalOrderDetailedService,WmsEstimatedArrivalOrderDetailed> {

    @ApiOperation(value = "ASN码盘 GET请求查询ASN码盘信息",response = JSONObject.class, notes = "GET请求查询ASN码盘信息")
    @PostMapping(value = "/seackASNSorting/{ssyqdid}")
    public @ResponseBody JSONObject getSeackASNSorting(@PathVariable("ssyqdid") String ssyqdid, HttpSession session) {
        List<WmsEstimatedArrivalOrderDetailed> tree = new ArrayList<WmsEstimatedArrivalOrderDetailed>();
        try {
            tree = baseService.seachASNBySsyqdid(ssyqdid);
        } catch (ServiceException e) {
            e.printStackTrace();
        }catch (Exception e) {
            e.printStackTrace();
        }
        return SetStatus.setSearchType(JSONArray.fromObject(tree).toString(),(int) tree.size());
    }

    @ApiOperation(value = "预期到货通知单明细 新增预期到货通知单明细",response = JSONObject.class, notes = "POST请求新增预期到货通知单明细")
    @PostMapping(value = "/add")
    public @ResponseBody
    JSONObject add(HttpSession session,@RequestBody WmsEstimatedArrivalOrderDetailed entity){
        try {
            //获取当期用户所属货主
            List<SysHz> hz=(List<SysHz>)session.getAttribute(BaseSessionInfo.SessionConsignor);
            if(hz.size()>0){
                entity.setHzid(hz.get(0).getHzid());
            }
            baseService.baseInsertSelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增预期到货通知单明细成功","", JSONObject.fromObject(entity).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增预期到货通知单明细失败",e.getMessage(),"");
        } catch (SucessException e) {
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"新增预期到货通知单明细成功","", JSONObject.fromObject(entity).toString());
        } catch (Exception e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"新增预期到货通知单明细失败",e.getMessage(),"");
        }
    }


    @PostMapping(value = "/list/{ssyqdid}")
    @ApiOperation(value = "预期到货通知单 根据ASNid获取明细", response = JSONObject.class,
            notes = "根据ASNid获取明细 ssyqdid 所属预期到货通知单ID")
    public @ResponseBody
    JSONArray list(HttpSession session,@PathVariable("ssyqdid") String ssyqdid) {
        JSONArray json = new JSONArray();
        try {
            json = JSONArray.fromObject(baseService.getBySsyqdid(ssyqdid));
        } catch (ServiceException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    @ApiOperation(value = "预期到货通知单明细 删除预期到货通知单明细",response = JSONObject.class, notes = "删除预期到货通知单明细")
    @PostMapping(value = "/delete")
    public @ResponseBody
    JSONObject delete(HttpSession session,@RequestBody SetAndModifyModel setAndModifyModel){
        try {
            baseService.delete(setAndModifyModel);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"删除预期到货通知单明细成功","", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"删除预期到货通知单明细成功",e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"删除预期到货通知单明细失败",e.getMessage(),"");
        } catch (Exception e){
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"删除预期到货通知单明细失败",e.getMessage(),"");
        }
    }

    @ApiOperation(value = "预期到货通知单明细 修改预期到货通知单明细",response = JSONObject.class, notes = "POST请求修改预期到货通知单明细")
    @PutMapping(value = "/edit")
    public @ResponseBody
    JSONObject edit(HttpSession session,@RequestBody WmsEstimatedArrivalOrderDetailed entity){
        try {
            baseService.baseUpdateByPrimaryKeySelective(entity);
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改预期到货通知单明细成功","", "");
        } catch (SucessException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_top.getType(),ReturnType.success.toString(),"修改预期到货通知单明细成功",e.getMessage(), "");
        } catch (ServiceException e) {
            e.printStackTrace();
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"修改预期到货通知单明细失败",e.getMessage(),"");
        } catch (Exception e){
            return SetStatus.setFunctionType(ReturnType.messageType_right.getType(),ReturnType.error.toString(),"修改预期到货通知单明细失败",e.getMessage(),"");
        }
    }
}
